﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using IPR_StudyMeetingMrgSys.ViewModel;
using IPR_StudyMeetingMrgSys.Models;
using IPR_StudyMeetingMrgSys.Enums;
using IPR_StudyMeetingMrgSys.Extension;
using IPR_StudyMeetingMrgSys.BLL;
using IPR_StudyMeetingMrgSys.Model;
using Newtonsoft.Json;
using log4net;
using System.Reflection;
using System.Data;

namespace IPR_StudyMeetingMrgSys.Controllers
{
    public class TrainPlanSendStepController : ApiController
    {
        ILog log = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

        /// <summary>
        /// 读取视图   计划下发阶段--下发部门表---部门表
        /// </summary>
        /// <param name="swhere"></param>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("api/TrainPlanSendStep/Search")]
        public IHttpActionResult Search(string swhere, int page, int limit)
        {
            ResultMsg resultMsg = new ResultMsg();
            try
            {
                swhere = swhere ?? string.Empty;
                int startindex = (page - 1) * limit + 1;
                int endindex = page * limit;
                if (!string.IsNullOrWhiteSpace(swhere))
                {
                    Dictionary<string, string> jsonObject = JsonConvert.DeserializeObject<Dictionary<string, string>>(swhere);
                    swhere = " 1=1 ";
                    foreach (var item in jsonObject)
                    {
                        if (item.Key == "TPID")
                        {
                            if ((item.Value != "") && (item.Value != null))
                                swhere += $" AND TPID={item.Value}";
                        }
                    }
                }

                bool blExist = false;
                int rowCount = 0;
                DataSet dsSearch = new IPR_StudyMeetingMrgSys.BLL.TrainPlanSendStep().GetListByPage2(swhere, "TPID", startindex, endindex);
                if (dsSearch != null)
                    if (dsSearch.Tables.Count > 0)
                        if (dsSearch.Tables[0].Rows.Count > 0)
                        {
                            //返回token信息
                            resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                            resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
                            resultMsg.Data = dsSearch.Tables[0];
                            resultMsg.Count = rowCount;
                            blExist = true;
                        }
                if (!blExist)
                {
                    //返回token信息
                    resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                    resultMsg.Info = "查询结果不存在";
                    resultMsg.Data = null;
                }
            }
            catch (Exception exc)
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                resultMsg.Info = StatusCodeEnum.Error.GetEnumText() + exc.Message;
                resultMsg.Data = "查询异常！" + exc.Message;
            }
            return Json(resultMsg);
        }



        [HttpPost]
        [Route("api/TrainPlanSendStep/Add")]
        public IHttpActionResult Add(ViewModel.VM_TrainPlanSendStep_Oper model)
        {
            //string sResult = "";
            Model.TrainPlanSendStep mTrainPlanSendStep = new Model.TrainPlanSendStep();
            ResultMsg resultMsg = new ResultMsg();
            try
            {
                mTrainPlanSendStep.SendID = new BLL.SysManage().GetMaxId("TrainPlanSendStep", "SendID");
                mTrainPlanSendStep.TPID = model.TPID;
                switch (LoginUser.DepLevel)
                {
                    case 0:
                        mTrainPlanSendStep.SendStep = "车间";
                        break;
                    default : mTrainPlanSendStep.SendStep = "车队"; break;
                }
                mTrainPlanSendStep.DeptID = LoginUser.DepID;
                mTrainPlanSendStep.DeptLevel = LoginUser.DepLevel;
                mTrainPlanSendStep.SendState =1;
                mTrainPlanSendStep.CreateDateTime = DateTime.Now;
                mTrainPlanSendStep.CreateUserID = LoginUser.UserInfo.UserID;
                mTrainPlanSendStep.CreateUserName = LoginUser.UserInfo.UserName;
                mTrainPlanSendStep.SendDateTime = DateTime.Now;
                mTrainPlanSendStep.SendUserID= LoginUser.UserInfo.UserID;
                
                //下发部门处理


                BLL.TrainPlanSendStep bllTrainPlanSendStep = new BLL.TrainPlanSendStep();
                string ErrorMsg = string.Empty;
                bllTrainPlanSendStep.Add(mTrainPlanSendStep,out ErrorMsg);
                BLL.TrainPlanSendDept trainPlanSendDept = new BLL.TrainPlanSendDept();
                if (string.IsNullOrEmpty(ErrorMsg))
                {
                    //成功处理
                    //培训计划不存在，添加新的培训计划
                    //增加主表
                    //bllTrainPlan.Add(mTrainPlanSendStep);

                    var listTrainPlanSendDept = new List<Model.TrainPlanSendDept>();
                    long i = new BLL.SysManage().GetMaxId("TrainPlanSendDept", "TPSDID");
                    foreach (var item in model.SendNextDeptIds)
                    {
                        i++;
                        Model.TrainPlanSendDept TrainPlanSendDeptModel = new Model.TrainPlanSendDept();
                        TrainPlanSendDeptModel.TPSDID =i;
                        TrainPlanSendDeptModel.TPID = mTrainPlanSendStep.TPID;
                        TrainPlanSendDeptModel.TPSSID = mTrainPlanSendStep.SendID;
                        TrainPlanSendDeptModel.TPSDDeptID = Convert.ToInt32(item);
                        listTrainPlanSendDept.Add(TrainPlanSendDeptModel);
                    }
                    trainPlanSendDept.AddList(listTrainPlanSendDept, mTrainPlanSendStep.SendID, mTrainPlanSendStep.TPID);



                    resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                    resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
                    resultMsg.Count = 1;
                    resultMsg.Data = "下发计划处理成功！";
                }
                else
                {
                    //失败处理
                    resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                    resultMsg.Info = StatusCodeEnum.Error.GetEnumText();
                    resultMsg.Data = ErrorMsg;
                }
            }
            catch (Exception exc)
            {

                resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                resultMsg.Info = StatusCodeEnum.Error.GetEnumText();
                resultMsg.Data = "培训计划增加异常！" + exc.Message;
            }
            return Json(resultMsg);
        }


        [HttpPost]
        [Route("api/TrainPlanSendStep/Edit")]
        public IHttpActionResult Edit(ViewModel.VM_TrainPlanSendStep_Oper model)
        {
            //string sResult = "";
            Model.TrainPlanSendStep mTrainPlanSendStep = new Model.TrainPlanSendStep();
            ResultMsg resultMsg = new ResultMsg();
            try
            {
                mTrainPlanSendStep.SendID =model.SendID;
                mTrainPlanSendStep.TPID = model.TPID;
                mTrainPlanSendStep.SendState = 1;
                mTrainPlanSendStep.SendDateTime = DateTime.Now;
                mTrainPlanSendStep.SendUserID = LoginUser.UserInfo.UserID;
                //下发部门处理
                BLL.TrainPlanSendStep bllTrainPlanSendStep = new BLL.TrainPlanSendStep();
                string ErrorMsg = string.Empty;
                BLL.TrainPlanSendDept trainPlanSendDept = new BLL.TrainPlanSendDept();
                bllTrainPlanSendStep.Update(mTrainPlanSendStep, out ErrorMsg);
                if (string.IsNullOrEmpty(ErrorMsg))
                {
                  
                    //成功处理
                    //培训计划不存在，添加新的培训计划
                    //增加主表
                    //bllTrainPlan.Add(mTrainPlanSendStep);
                    Model.TrainPlanSendDept TrainPlanSendDeptModel = new Model.TrainPlanSendDept();
                    var listTrainPlanSendDept = new List<Model.TrainPlanSendDept>();
                    long i = new BLL.SysManage().GetMaxId("TrainPlanSendDept", "TPSDID");
                    foreach (var item in model.SendNextDeptIds)
                    {
                        i++;
                        Model.TrainPlanSendDept objSendDept = new Model.TrainPlanSendDept();
                        objSendDept.TPSDID = i;
                        objSendDept.TPID = mTrainPlanSendStep.TPID;
                        objSendDept.TPSSID = mTrainPlanSendStep.SendID;
                        objSendDept.TPSDDeptID = Convert.ToInt32(item);
                        listTrainPlanSendDept.Add(objSendDept);
                    }
                    trainPlanSendDept.AddList(listTrainPlanSendDept, mTrainPlanSendStep.SendID, mTrainPlanSendStep.TPID);

                    resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                    resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
                    resultMsg.Count = 1;
                    resultMsg.Data = "下发计划修改成功！";
                }
                else
                {
                    //失败处理
                    resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                    resultMsg.Info = StatusCodeEnum.Error.GetEnumText();
                    resultMsg.Data = ErrorMsg;
                }
            }
            catch (Exception exc)
            {

                resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                resultMsg.Info = StatusCodeEnum.Error.GetEnumText();
                resultMsg.Data = "培训计划增加异常！" + exc.Message;
            }
            return Json(resultMsg);
        }

        [HttpGet]
        [Route("api/TrainPlanSendStep/DelList")]
        public IHttpActionResult DelList(string TPIDlist)
        {
            //log.Info(JsonConvert.SerializeObject(delidlist));
            ResultMsg resultMsg = new ResultMsg();
            try
            {
                var row = new IPR_StudyMeetingMrgSys.BLL.TrainPlanSendStep().DeleteListByTPID(TPIDlist);
                if (row>0)
                {
                    resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                    resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
                    resultMsg.Data = "下发计划删除成功！";
                }
                else
                {
                    resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                    resultMsg.Data = "删除成功，删除0行";
                }
            }
            catch (Exception exc)
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                resultMsg.Info = StatusCodeEnum.Error.GetEnumText();
                resultMsg.Data = "删除时出现异常！" + exc.Message;
            }
            return Json(resultMsg);
        }
    }
}
